import Mock from 'mockjs'

// 模拟用户数据
const users = [
  {
    id: 1,
    username: 'admin',
    password: '123456',
    email: 'admin@gongyinlian.com',
    phone: '13800138000',
    realName: '系统管理员',
    avatar: 'https://avatars.githubusercontent.com/u/1?v=4',
    role: 'admin',
    status: 'active',
    lastLoginTime: '2024-01-15 10:30:00',
    createTime: '2024-01-01 00:00:00'
  },
  {
    id: 2,
    username: 'manager',
    password: '123456',
    email: 'manager@gongyinlian.com',
    phone: '13800138001',
    realName: '部门经理',
    avatar: 'https://avatars.githubusercontent.com/u/2?v=4',
    role: 'manager',
    status: 'active',
    lastLoginTime: '2024-01-15 09:15:00',
    createTime: '2024-01-01 00:00:00'
  },
  {
    id: 3,
    username: 'employee',
    password: '123456',
    email: 'employee@gongyinlian.com',
    phone: '13800138002',
    realName: '普通员工',
    avatar: 'https://avatars.githubusercontent.com/u/3?v=4',
    role: 'employee',
    status: 'active',
    lastLoginTime: '2024-01-15 08:45:00',
    createTime: '2024-01-01 00:00:00'
  }
]

// 登录接口
Mock.mock('/api/v1/auth/login', 'post', (options) => {
  const { username, password } = JSON.parse(options.body)
  
  // 查找用户
  const user = users.find(u => u.username === username && u.password === password)
  
  if (user) {
    // 生成token
    const token = Mock.Random.string('lower', 32)
    const refreshToken = Mock.Random.string('lower', 32)
    
    // 更新最后登录时间
    user.lastLoginTime = Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
    
    return {
      code: 200,
      message: '登录成功',
      data: {
        token,
        refreshToken,
        expiresIn: 7200,
        userInfo: {
          id: user.id,
          username: user.username,
          email: user.email,
          phone: user.phone,
          realName: user.realName,
          avatar: user.avatar,
          role: user.role,
          status: user.status
        }
      }
    }
  } else {
    return {
      code: 401,
      message: '用户名或密码错误',
      data: null
    }
  }
})

// 注册接口
Mock.mock('/api/v1/auth/register', 'post', (options) => {
  const userData = JSON.parse(options.body)
  
  // 检查用户名是否已存在
  const existingUser = users.find(u => u.username === userData.username)
  if (existingUser) {
    return {
      code: 400,
      message: '用户名已存在',
      data: null
    }
  }
  
  // 检查邮箱是否已存在
  const existingEmail = users.find(u => u.email === userData.email)
  if (existingEmail) {
    return {
      code: 400,
      message: '邮箱已被注册',
      data: null
    }
  }
  
  // 创建新用户
  const newUser = {
    id: users.length + 1,
    username: userData.username,
    password: userData.password,
    email: userData.email,
    phone: userData.phone,
    realName: userData.realName || userData.username,
    avatar: `https://avatars.githubusercontent.com/u/${users.length + 1}?v=4`,
    role: userData.userType || 'employee',
    status: 'active',
    companyName: userData.companyName,
    department: userData.department,
    position: userData.position,
    birthday: userData.birthday,
    lastLoginTime: null,
    createTime: Mock.Random.datetime('yyyy-MM-dd HH:mm:ss')
  }
  
  users.push(newUser)
  
  return {
    code: 200,
    message: '注册成功',
    data: {
      userId: newUser.id,
      username: newUser.username
    }
  }
})

// 登出接口
Mock.mock('/api/v1/auth/logout', 'post', () => {
  return {
    code: 200,
    message: '登出成功',
    data: null
  }
})

// 刷新token接口
Mock.mock('/api/v1/auth/refresh', 'post', (options) => {
  const { refreshToken } = JSON.parse(options.body)
  
  if (refreshToken) {
    const newToken = Mock.Random.string('lower', 32)
    const newRefreshToken = Mock.Random.string('lower', 32)
    
    return {
      code: 200,
      message: 'Token刷新成功',
      data: {
        token: newToken,
        refreshToken: newRefreshToken,
        expiresIn: 7200
      }
    }
  } else {
    return {
      code: 401,
      message: 'RefreshToken无效',
      data: null
    }
  }
})

// 获取当前用户信息
Mock.mock('/api/v1/auth/profile', 'get', () => {
  // 这里应该从token中解析用户信息，mock中返回默认管理员信息
  const user = users[0]
  
  return {
    code: 200,
    message: '获取成功',
    data: {
      id: user.id,
      username: user.username,
      email: user.email,
      phone: user.phone,
      realName: user.realName,
      avatar: user.avatar,
      role: user.role,
      status: user.status,
      companyName: user.companyName,
      department: user.department,
      position: user.position,
      birthday: user.birthday,
      lastLoginTime: user.lastLoginTime,
      createTime: user.createTime
    }
  }
})

// 修改密码接口
Mock.mock('/api/v1/auth/change-password', 'post', (options) => {
  const { oldPassword, newPassword } = JSON.parse(options.body)
  
  // 验证旧密码（mock中假设旧密码是123456）
  if (oldPassword !== '123456') {
    return {
      code: 400,
      message: '原密码错误',
      data: null
    }
  }
  
  // 更新密码
  users.forEach(user => {
    if (user.username === 'admin') {
      user.password = newPassword
    }
  })
  
  return {
    code: 200,
    message: '密码修改成功',
    data: null
  }
})

// 忘记密码接口
Mock.mock('/api/v1/auth/forgot-password', 'post', (options) => {
  const { email } = JSON.parse(options.body)
  
  const user = users.find(u => u.email === email)
  if (!user) {
    return {
      code: 400,
      message: '该邮箱未注册',
      data: null
    }
  }
  
  // 生成重置码
  const resetCode = Mock.Random.string('number', 6)
  
  return {
    code: 200,
    message: '重置码已发送到您的邮箱',
    data: {
      resetCode,
      email: user.email
    }
  }
})

// 重置密码接口
Mock.mock('/api/v1/auth/reset-password', 'post', (options) => {
  const { email, resetCode, newPassword } = JSON.parse(options.body)
  
  const user = users.find(u => u.email === email)
  if (!user) {
    return {
      code: 400,
      message: '用户不存在',
      data: null
    }
  }
  
  // 验证重置码（mock中假设重置码是123456）
  if (resetCode !== '123456') {
    return {
      code: 400,
      message: '重置码错误',
      data: null
    }
  }
  
  // 更新密码
  user.password = newPassword
  
  return {
    code: 200,
    message: '密码重置成功',
    data: null
  }
}) 